package com.hotelmanage.manage.controllers;


import com.hotelmanage.manage.entities.Room;
import com.hotelmanage.manage.entities.Student;
import com.hotelmanage.manage.services.RoomService;
import com.hotelmanage.manage.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("rooms")
public class RoomController {

    @Autowired
    RoomService roomService;

    @Autowired
    StudentService studentService;

    @GetMapping
    public String displayStudents(Model model)
    {
        List<Room> rooms = roomService.getAll();
        model.addAttribute("rooms",rooms);
        return "rooms/list-rooms";
    }

    @GetMapping("/new")
    public String displayRoomForm(Model model){
        Room aRoom = new Room();
        List<Student> students = studentService.getAll();
        model.addAttribute("room",aRoom);
        model.addAttribute("allStudents",students);
        return "rooms/new-room";
    }

    @PostMapping("/save")
    public String createRoom(Model model, Room room){
        //处理保存到数据库的行为
        roomService.save(room);

        //使用重定位防止重复提交
        return "redirect:/rooms";

    }

}
